<template>
    <div class="app-container">
        <!-- 预约规则添加 -->
        <el-form label-width="120px">
          <el-form-item label="医院编号">
              <el-input v-model="sche.hoscode"/>
          </el-form-item>
          <el-form-item label="科室编号">
              <el-input v-model="sche.depcode"/>
          </el-form-item>
          <el-form-item label="职称">
              <el-input v-model="sche.title"/>
          </el-form-item>
          <el-form-item label="医生名称">
              <el-input v-model="sche.docname"/>
          </el-form-item>
          <el-form-item label="排班日期">
            <el-date-picker
                v-model="sche.workDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="排班时间">
              <template>
                <el-radio v-model="workTime" label="0">上午</el-radio>
                <el-radio v-model="workTime" label="1">下午</el-radio>
              </template>
          </el-form-item>
          <el-form-item label="可预约数">
              <el-input v-model="sche.reservedNumber"/>
          </el-form-item>
          <el-form-item label="剩余预约数">
              <el-input v-model="sche.availableNumber"/>
          </el-form-item>
          <el-form-item label="挂号费">
              <el-input v-model="sche.amount"/>
          </el-form-item>
          <el-form-item label="排班状态">
              <template>
                <el-radio v-model="status" label="-1">停诊</el-radio>
                <el-radio v-model="status" label="0">停约</el-radio>
                <el-radio v-model="status" label="1">可约</el-radio>
              </template>
          </el-form-item>
          <el-form-item label="排班编号">
              <el-input v-model="sche.hosScheduleId"/>
          </el-form-item>
          <el-form-item label="擅长技能">
            <el-input type="textarea" v-model="sche.skill" :rows="3"/>
          </el-form-item>

          <el-form-item label="">
              <el-button type="primary" @click="saveOrUpdate">保存</el-button>
          </el-form-item>
        </el-form>
    </div>
</template>
<script>
import schedule from '@/api/schedule'

export default {
    data() {
        return {
            sche: {

            },
            workTime: '0',
            status: '-1'
        }
    },
    created() {//页面渲染之前执行
        if(this.$route.params && this.$route.params.id) {
            //获取路由中的id
            const id = this.$route.params.id
            //根据id查询信息
            this.findById(id)
        } else {
            //清空表单数据
            this.booking = {}
        }
    },
    methods: {
        //添加
        save() {
            //此时是新增
            schedule.saveSchedule(this.sche)
            .then((response) => {
                //提示信息
                this.$message({
                    type: 'success',
                    message: '添加成功！'
                })
                //跳转到列表，使用路由跳转
                this.$router.push({path: '/schedule/list'})
            })
        },
        //修改
        update() {
            //此时是修改
            schedule.updateSchedule(this.sche)
            .then((response) => {
                //提示信息
                this.$message({
                    type: 'success',
                    message: '修改成功！'
                })
                //跳转到列表，使用路由跳转
                this.$router.push({path: '/schedule/list'})
            })
        },
        //添加方法
        saveOrUpdate() {
            // 赋值
            this.sche.workTime = this.workTime
            this.sche.status = this.status
            if(!this.sche.id) {
                this.save()
            } else {
                this.update()
            }
        },
        //根据id查询数据
        findById(id) {
            schedule.findById(id)
            .then((response) => {
                this.sche = response.data
                // 赋值
                this.workTime = '' + this.sche.workTime
                this.status = '' + this.sche.status
            })
        }
    }
}
</script>
